<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Ueditor控制器
 */
class Ue extends CI_Controller
{
    function __construct(){
        parent::__construct();
    }

    function index(){
        //$path = APPPATH.'third_party/ueditor/';
        //$path = APPPATH.'/third_party/ueditor/php/';
        $path = 'static/ueditor/php/';

        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($path."config.json")), true);
        $action = $_GET['action'];

        switch ($action) {
            case 'config':
                $result =  json_encode($CONFIG);
                break;

            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = include_once($path."action_upload.php");
                break;

            /* 列出图片 */
            case 'listimage':
                $result = include_once($path."action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include_once($path."action_list.php");
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = include_once($path."action_crawler.php");
                break;

            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                $result = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                $result = json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        }

            $this->output->set_output($result);
    }

}